package com.usiful.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.usiful.model.FollowSale;
import com.usiful.model.Product;

/**
 * 
 * @author [周织能]
 * @description excel工具
 * @date 2016年7月24日
 */
public class UsifulExcelHelper {

	private static UsifulExcelHelper instance;

	private UsifulExcelHelper() {

	}

	public static UsifulExcelHelper getInstance() {
		if (instance == null) {
			instance = new UsifulExcelHelper();
		}
		return instance;
	}

	/**
	 * 创建被跟卖产品excel数据表
	 * 
	 * @param fileName
	 * @param followSales
	 */
	public void createFollowSaleExcel(String fileName, List<Product> products) {
		// 创建一个excel文件   
		HSSFWorkbook wb = new HSSFWorkbook();
		FileOutputStream fileOut;
		int index = 0;
		for (int t = 0; t < products.size(); t++) {
			Product product = products.get(t);
			Set<FollowSale> followSaleSet = product.getFollowSales();
			if (followSaleSet == null || followSaleSet.size() == 0) {
				continue;
			}
			List<FollowSale> followSales = new ArrayList<FollowSale>(product.getFollowSales());
			++index;
			HSSFSheet sheet = wb.createSheet(index + "_" + product.getName());
			HSSFRow row = sheet.createRow((short) 0); // 创建标题行
			for (int i = 0; i < FollowSale.concernFiles.length; i++) {
				HSSFCell cell = row.createCell(i, HSSFCell.CELL_TYPE_STRING);
				cell.setCellValue(FollowSale.concernFiles[i]);
			}

			HSSFRow newRow;
			// "名称","描述","运费","售价","总价","销量","店铺名","店铺链接","产品链接","代号","快递时间"
			HSSFCell nameCell, descCell, shipPriceCell, salePriceCell, priceCell, salesCell, shopNameCell, shopUrlCell,
					productUrlCell, magicCell,shipTimeCell;
			int rowIdx = 0;
			for (int j = 0; j < followSales.size(); j++) {
				rowIdx = j + 1;
				newRow = sheet.createRow(rowIdx); // 创建具体信息行
				
				nameCell = newRow.createCell(0, HSSFCell.CELL_TYPE_STRING);
				descCell = newRow.createCell(1, HSSFCell.CELL_TYPE_STRING);
				shipPriceCell = newRow.createCell(2, HSSFCell.CELL_TYPE_NUMERIC);
				salePriceCell = newRow.createCell(3, HSSFCell.CELL_TYPE_NUMERIC);
				priceCell = newRow.createCell(4, HSSFCell.CELL_TYPE_NUMERIC);
				salesCell = newRow.createCell(5, HSSFCell.CELL_TYPE_NUMERIC);
				shopNameCell = newRow.createCell(6, HSSFCell.CELL_TYPE_STRING);
				shopUrlCell = newRow.createCell(7, HSSFCell.CELL_TYPE_STRING);
				productUrlCell = newRow.createCell(8, HSSFCell.CELL_TYPE_STRING);
				magicCell = newRow.createCell(9, HSSFCell.CELL_TYPE_STRING);
				shipTimeCell = newRow.createCell(10,HSSFCell.CELL_TYPE_STRING);
				
				nameCell.setCellValue(followSales.get(j).getName());
				descCell.setCellValue(followSales.get(j).getDecription());
				shipPriceCell.setCellValue((double) followSales.get(j).getShipPrice());
				salePriceCell.setCellValue((double) followSales.get(j).getSalePrice());
				priceCell.setCellValue((double) followSales.get(j).getPrice());
				salesCell.setCellValue((double) followSales.get(j).getSales());
				shopNameCell.setCellValue(followSales.get(j).getShop());
				shopUrlCell.setCellValue(followSales.get(j).getUrl());
				productUrlCell.setCellValue(followSales.get(j).getProductLink());
				magicCell.setCellValue(followSales.get(j).getMagic());
				shipTimeCell.setCellValue(followSales.get(j).getShipTime());
			}

		}
		try {
			fileOut = new FileOutputStream(fileName);
			wb.write(fileOut);
			fileOut.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
